package utils

import (
	"fmt"
	"github.com/lifei6671/gocaptcha"
	uuid "github.com/satori/go.uuid"
	"os"
	"strings"
	"time"
)

func GenerateUuid() string {
	var (
		u1 uuid.UUID
	)
	u1 = uuid.Must(uuid.NewV4(), nil)
	return strings.ReplaceAll(fmt.Sprintf("%s", u1), "-", "")
}

func GenerateTimeStampNano() string {
	var (
		timestamp int64
	)
	timestamp = time.Now().UnixNano()
	return fmt.Sprintf("%d", timestamp)
}

func GenerateTimeStamp() string {
	var (
		timestamp int64
	)
	timestamp = time.Now().Unix()
	return fmt.Sprintf("%d", timestamp)
}

func GenerateCaptcha(timestamp string) (string, error) {
	var (
		text string
		f    *os.File
		err  error
	)
	if err = gocaptcha.ReadFonts("./media/fonts", ".ttf"); err != nil {
		return "", err
	}

	captchaImage := gocaptcha.NewCaptchaImage(300, 300, gocaptcha.RandLightColor())
	captchaImage.DrawNoise(gocaptcha.CaptchaComplexLower)
	captchaImage.DrawTextNoise(gocaptcha.CaptchaComplexLower)
	text = gocaptcha.RandText(4)
	captchaImage.DrawText(text)
	captchaImage.DrawLine(3)
	captchaImage.DrawBorder(gocaptcha.ColorToRGB(0x17A7A7A))
	//captchaImage.DrawSineLine()
	captchaImage.DrawHollowLine()
	if f, err = os.Create(fmt.Sprintf("./media/images/%s.jpg", timestamp)); err != nil {
		return "", err
	} else {
		defer f.Close()
	}
	captchaImage.SaveImage(f, gocaptcha.ImageFormatJpeg)
	return text, nil
}
